home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2010 Summer - Disc 1 / WN_Ete2010_CD1.iso / Onglet5 / Weezo / Weezo setup.exe / {code_appDir} / www / local / updateVersion.php < prev    next >
PHP Script  |  2010-05-19  |  15KB  |  395 lines

  1. <?php
  2. /**
  3.  * New weezo version available message box
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @category   NA
  14.  * @package    NA
  15.  * @author     Nicolas Bruley / Peer 2 World <contact@weezo.net>
  16.  * @copyright  2005-2009 Nicolas Bruley / Peer 2 World
  17.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  18.  * @version    CVS: $Id:$
  19.  * @link       http://www.weezo.net
  20.  * @since      File available since Release 1.0.0
  21.  */
  22.  
  23. $updateHost=substr(DOWNLOAD_SITE,7);
  24.  
  25. $updateUri='/update.zip';
  26.  
  27. $_ENV['7zDecOutput']='';
  28.  
  29. // Initialize script data
  30. require_once('localFunctions.php');
  31. lfInit();
  32.  
  33. function updatePage($id,$text){
  34.     if($id) echo '<script type="text/javascript">document.getElementById("'.$id.'").innerHTML="'.str_replace('"','\\"',$text).'"</script>';
  35.     else echo '<script type="text/javascript">'.$text.'</script>';
  36.     flush();
  37. }
  38.  
  39. /**
  40.  * @desc display update error page
  41.  *
  42.  */
  43. function displayErrorPage($extraMessage=''){
  44.     updatePage('mainCaption','<span class="warning">'.cfCaption('updateVersionError').$extraMessage.'</span>');
  45.     updatePage('cancelButtonOuter',outButton(cfCaption('genCancel'),'javascript:wl.UICommand(\'close\')',outIcon('cancel'),false,false,'style="margin-bottom:5px"'));
  46.     exit;
  47. }
  48.  
  49.  
  50.  
  51. /**
  52.  * Save preferences
  53.  */
  54. if(isset($_POST['savePrefs']) && ($_POST['savePrefs']=='auto' || $_POST['savePrefs']=='disabled')){
  55.     cfServerSendCommand('changeParameter name="updateApplication" value="'.$_POST['savePrefs'].'"');
  56. }
  57.  
  58.  
  59. // Proceed to download
  60. if(isset($_GET['noConfirmation']) || cfGGetVar('updateApplication')=='auto' || (isset($_POST['doUpdate']) && $_POST['doUpdate']=='download')){
  61.     // Display download page
  62.     cfInsertHEAD(false);
  63.     echo '<meta width="440x175"></meta>';
  64.     echo '<script type="text/javascript">function uPB(pc){progressSetPerc("pBar",pc);progressSetText("pBar",pc+"%")}</script></head>';
  65.     echo '<body width="440" height="175" title="Weezo" oncontextmenu="return false;" oonload="document.proceedForm.submit()">';
  66.     echo outDivFrame('frame2',false,'margin-top:10px');
  67.  
  68.     echo outImage(outIcon('installIcon'),false,false,'float:left; margin-right:1em');
  69.     echo '<b>'.cfCaption('newVersionAvailable').'</b>';
  70.     echo '<br/><br/><div id="mainCaption">'.cfCaption('updateVersionDownloading').'</div><br/>';
  71.     echo '<div id="progress" style="text-align:center">';
  72.     echo outProgressBar(0,'200px','0%','pBar');
  73.     echo '</div><br/>';
  74.     echo '<center id="cancelButtonOuter">'.outButton(cfCaption('genCancel'),'javascript:wl.UICommand(\'close\')',outIcon('cancel'),false,'cancelButton','style="margin-bottom:5px"').'</center><br/>';
  75.     echo '<form name="proceedForm" method="POST" action="" style="display:none"><input name="doUpdate" value="download"></form>';
  76.     echo '</div>';
  77.     flush();
  78.  
  79.  
  80.     /**
  81.      * Update update script
  82.      */
  83.     if(isset($_GET['updateScript']) && $_GET['updateScript']!=substr(md5_file($_SERVER['SCRIPT_FILENAME']),0,3)){
  84.         updatePage('mainCaption','Updating script...');
  85.         // Download script
  86.         if(function_exists('cfSocketHTTPRequest')) $scriptContent=cfSocketHTTPRequest('http://'.$updateHost.'/updateVersion.phps'); else    $scriptContent=@file_get_contents('http://'.$updateHost.'/updateVersion.phps');
  87.         if(!strpos($scriptContent,'?>')) displayErrorPage('(Update script couldn\'t be downloaded)');
  88.         // Save script
  89.         file_put_contents(cfAppDocRoot().'/local/updateVersion.php',$scriptContent);
  90.         updatePage('mainCaption','Script successfully updated');
  91.  
  92.         // Reload page (to use downloaded script)
  93.         updatePage(false,'var nl=document.location.href;document.location=nl.replace(/updateScript/,"updateScriptOK")+"&noConfirmation=1"');
  94.         exit;
  95.     }
  96.  
  97.     /**
  98.      * Download update package
  99.      */
  100. //if(!isset($_GET['test'])){
  101.     set_time_limit(3600);
  102.  
  103.     // Destroy previous file
  104.     if(file_exists(cfAppDataDir().'/temp/update.zip')) {
  105.         if(!@unlink(cfAppDataDir().'/temp/update.zip')) displayErrorPage('(Cannot remove previous update package)');
  106.     }
  107.  
  108.     // Destroy previous file
  109.     if(file_exists(cfAppDataDir().'/temp/update.7z')) {
  110.         if(!@unlink(cfAppDataDir().'/temp/update.7z')) displayErrorPage('(Cannot remove previous update package)');
  111.     }
  112.  
  113.     // Check file's existence and size
  114.     if(!isset($_POST['updateFile']) && isset($_GET['updateFile'])) $_POST['updateFile']=$_GET['updateFile'];
  115.     if(isset($_POST['updateFile']) && eregi('^[0-9a-z?&=/\.]{1,100}$',$_POST['updateFile'])) $updateUri=$_POST['updateFile'];
  116.     if($updateUri[0]!='/') $updateUri='/'.$updateUri;
  117.  
  118.     // Local downloaded file name
  119.     $localUpdateFile=cfAppDataDir().'/temp/update.'.((strpos($updateUri,'zip'))?'zip':'7z');
  120.  
  121.     // Get HTTP response header (1024 first chars) to :
  122.     // - follow redirections
  123.     // - get file size from content-length header
  124.  
  125.     // Max 2 redirections
  126.     $proceed=2;
  127.     while($proceed){
  128.         if(!($handle=cfSocketHTTPRequest('http://'.$updateHost.$updateUri,7,false,$foo1,$foo2,false,true))) displayErrorPage('(Cannot connect to download site)');
  129.         $response='';
  130.         while(strlen($response)<1023 && !feof($handle)) $response .= fread($handle, 1024);
  131.  
  132.         $pos=strpos($response,"\r\n\r\n");
  133.         if($pos === false){
  134.             fclose($handle);
  135.             displayErrorPage('(Cannot find download package)');
  136.         }
  137.         $header=substr($response,0,$pos);
  138.         $response=substr($response,$pos+4);
  139.  
  140.         $headers = array();
  141.         $lines = explode("\r\n", $header);
  142.         $fSize=0;
  143.         if(count($lines)==0) displayErrorPage('(Cannot find download package)');
  144.  
  145.         // HTTP response code
  146.         $responseCode=substr($lines[0],9,3);
  147.  
  148.         // Seek file size or redirection
  149.         foreach($lines as $value){
  150.             if(strtolower(substr($value,0,15))=='content-length:') $fSize=trim(substr($value,15));
  151.             if(strtolower(substr($value,0,9))=='location:') $redir=trim(substr($value,9));
  152.         }
  153.         // If redirection
  154.         if(isset($redir)) {
  155.             $proceed--;
  156.             if($proceed<0) displayErrorPage('(Too many redirections)');
  157.             $updateUri=$redir;
  158.             if(substr($redir,0,1)!='/') $updateHost='';
  159.             if(substr($updateHost,0,7)=='http://') $updateHost=substr($updateHost,7);
  160.             if(substr($updateUri,0,7)=='http://') $updateUri=substr($updateUri,7);
  161.             unset($redir);
  162.         }
  163.         // ! 200 OK
  164.         elseif($responseCode!='200') displayErrorPage('(Cannot find download package - '.$responseCode.')');
  165.         // No redirection and response code 200 OK
  166.         else $proceed=0;
  167.     }
  168.  
  169.     // Proceed Download
  170.     $handlew = fopen ($localUpdateFile, "w");
  171.     if(!$handlew) {fclose($handle);displayErrorPage('(Cannot open local temporary update file)');}
  172.     $i=-1;
  173.     fwrite($handlew,$response);
  174.     $dlSize=strlen($response);
  175.     while (!feof($handle)) {
  176.         $response=fread($handle, 8192);
  177.         fwrite($handlew,$response);
  178.         $i++;$dlSize+=strlen($response);
  179.         if($i%9==0) updatePage(false,'uPB('.floor(100*$dlSize/$fSize).');');
  180.     }
  181.     fclose($handlew);
  182.     fclose($handle);
  183. //}
  184.     updatePage('mainCaption',cfCaption('updateVersionUnzip'));
  185.     updatePage('progress','<br/>');
  186.  
  187.     /**
  188.      * UNZIP / 7ZDec in /temp/update
  189.      */
  190.     $i=0;
  191.     $unzipOk=false;
  192.  
  193.     @mkdir(cfAppDataDir().'/temp/update/');
  194.     $df=cfAppDataDir().'/temp/update/';
  195.  
  196.  
  197.     // Try to open as zip archive
  198.     if(cfFileExtension($localUpdateFile)=='zip' && ($zip = @zip_open($localUpdateFile))) {
  199.         while ($zip_entry = zip_read($zip)) {
  200.             $i++;updatePage('progress',$i.' '.cfCaption('genFiles'));
  201.  
  202.             if (zip_entry_open($zip, $zip_entry, 'r')) {
  203.                 if(substr(zip_entry_name($zip_entry),-1)=='/') $dirname=$df.zip_entry_name($zip_entry);
  204.                 else $dirname=dirname($df.zip_entry_name($zip_entry)).'/';
  205.                 if(!is_dir($dirname)) cfMkdir($dirname);
  206.                 if(!is_dir($df.zip_entry_name($zip_entry)) && $handle=fopen($df.zip_entry_name($zip_entry),'w')){
  207.                     @fwrite($handle,zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
  208.                     fclose($handle);
  209.                 }
  210.                 zip_entry_close($zip_entry);
  211.             }
  212.         }
  213.         zip_close($zip);
  214.         $unzipOk=true;
  215.     }
  216.     // If not zip archive, try to open with 7zDec
  217.     elseif(cfFileExtension($localUpdateFile)=='7z'){
  218.         $decExe=cfAppBinDir().'/7zDec.exe';
  219.         // Download 7z decoder if not installed
  220.         if(!file_exists($decExe)){
  221.             if(function_exists('cfSocketHTTPRequest')) $decBin=cfSocketHTTPRequest('http://'.$updateHost.'/7zDec.exe'); else    $decBin=@file_get_contents('http://'.$updateHost.'/7zDec.exe');
  222.             if(strlen($decBin)>20000 && strlen($decBin)<30000) {
  223.                 $decExe=cfAppDataDir().'/temp/7zDec.exe';
  224.                 @file_put_contents($decExe,$decBin);
  225.                 unset($decExe);
  226.             }
  227.             else displayErrorPage('(Cannot download decompacter)');
  228.         }
  229.         // Launch 7z decoder
  230.         if(file_exists($decExe)){
  231.             /**
  232.              * @desc Update decompression progress from 7zDec output parsing
  233.              *
  234.              * @param string $buff
  235.              */
  236.             function update7zProgress($buff){
  237.                 static $nb=0;
  238.                 str_replace("> Extracting",'',$buff,$rc);
  239.                 if($rc>0) {
  240.                     $nb+=$rc;
  241.                     updatePage('progress',($nb).' '.cfCaption('genFiles'));
  242.                 }
  243.                 $_ENV['7zDecOutput'].=$buff;
  244.             }
  245.             chdir(cfAppDataDir().'/temp/update');
  246.             cfStreamProc('"'.cfAppBinDir().'/7zDec.exe" e "'.$localUpdateFile.'"',array('stdout'=>'update7zProgress'),array('bufferSize'=>30,'haltOnSeq'=>'Ok'.chr(13).chr(10)));
  247.             // Look in 7zDec output if decompression is successfull
  248.             if(strpos($_ENV['7zDecOutput'],'Everything is Ok')!==false) $unzipOk=true;
  249.         }
  250.     }
  251.  
  252.     // destroy update package
  253.     @unlink($localUpdateFile);
  254.  
  255.     // If decompression successfull
  256.     if($unzipOk){
  257.         // Extra update script
  258.         if(file_exists(cfAppDataDir().'/temp/update/extraUpdate.php')) {
  259.             require(cfAppDataDir().'/temp/update/extraUpdate.php');
  260.             @unlink(cfAppDataDir().'/temp/update/extraUpdate.php');
  261.         }
  262.  
  263.         /**
  264.          * Move unzipped files
  265.          */
  266.         
  267.         // If a new "Weezo Updater.exe" is in the downloaded package, replace old one
  268.         if(file_exists(cfAppDataDir().'/temp/update/bin/Weezo Updater.exe')) {
  269.             // Normal Weezo installation : Weezo Updater is located right into application data root dir (cfAppDataRootDir())
  270.             if(file_exists(cfAppDataRootDir().'/Weezo Updater.exe')){
  271.                 if(!@unlink(cfAppDataRootDir().'/Weezo Updater.exe')) die('Update failure: cannot delete file "'.cfAppDataRootDir().'/Weezo Updater.exe"<br>Download full installer to upgrade');
  272.                 @rename(cfAppDataDir().'/temp/update/bin/Weezo Updater.exe',cfAppDataRootDir().'/Weezo Updater.exe');
  273.             }
  274.             // Compact Weezo installation : Weezo Updater is located into bin directory
  275.             else{
  276.                 if(!@unlink(cfAppBinDir().'/Weezo Updater.exe')) {
  277.                     // If update failed, retry in cfAppDataRootDir
  278.                     if(!@rename(cfAppDataDir().'/temp/update/bin/Weezo Updater.exe',cfAppDataRootDir().'/Weezo Updater.exe'))
  279.                         die('Update failure: cannot delete file "'.cfAppBinDir().'/Weezo Updater.exe"<br>Download full installer to upgrade');
  280.                 }
  281.                 else @rename(cfAppDataDir().'/temp/update/bin/Weezo Updater.exe',cfAppBinDir().'/Weezo Updater.exe');
  282.             }
  283.         }
  284.  
  285.         // Move subversion file if existing, for Normal Weezo installation
  286.         /* Not needed anymore since of V1.1.1 as subVersion is now located in /www
  287.         if(file_exists(cfAppDataDir().'/temp/update/bin/subversion') && file_exists(cfAppDataRootDir().'/subversion')){
  288.             if(!@unlink(cfAppDataRootDir().'/subversion')) die('Update failure: cannot delete file "'.cfAppDataRootDir().'/subversion"<br>Download full installer to upgrade');
  289.             @rename(cfAppDataDir().'/temp/update/bin/subversion',cfAppDataRootDir().'/subversion');
  290.         }
  291.         */
  292.  
  293.         // Move www directory
  294.         if(is_dir(cfAppDataDir().'/temp/update/www')) cfCopyDir(cfAppDataDir().'/temp/update/www',cfAppDataRootDir().'/www',true);
  295.  
  296.         // Move data directory
  297.         if(is_dir(cfAppDataDir().'/temp/update/data')) cfCopyDir(cfAppDataDir().'/temp/update/data',cfAppDataRootDir().'/data',true);
  298.  
  299.         // If all files have been moved, just close window, don't reboot
  300.         if(!count(glob(cfAppDataDir().'/temp/update/*'))){
  301.             @unlink(cfAppDataDir().'/temp/update');
  302.  
  303.             cfInsertHEAD();
  304.             die('<body onload="wl.UICommand(\'close\')"></body>');
  305.         }
  306.  
  307.         /**
  308.          * REBOOT
  309.          * If files must be written inside cfAppRootDir(),
  310.          * use Weezo Updater.exe (with admin rights) to complete installation
  311.          * (called by weezo.exe, as it couldn't work properly if lauched by apache)
  312.          */
  313.  
  314.         // Close window and relaunch Weezo
  315.         updatePage('mainCaption',cfCaption('genRestarting'));
  316.         updatePage('progress','<br/>');
  317.         updatePage(false,'document.getElementById("cancelButton").className="buttonDisabled";');
  318.         updatePage(false,'document.getElementById("cancelButton").href="#";');
  319.  
  320.         sleep(1);
  321.         cfServerSendCommand('installUpdate'); // Note: customExecFork(cfAppBinDir().'/weezoLauncher.exe','SW_HIDE','-installUpdate'); Doesn't work !!!!
  322.  
  323.         exit;
  324.     }
  325.     echo '<script type="text/javascript">dgi("mainCaption").innerHTML="'.cfCaption('updateVersionError').'"</script>';
  326.  
  327.     echo '<script type="text/javascript">document.location="close:"</script>';
  328.     exit;
  329. }
  330.  
  331.  
  332. // Udate disabled : quit
  333. if(cfGGetVar('updateApplication')=='disabled'){
  334.     echo '<body onload="wl.UICommand(\'close\')"></body>';
  335.     exit;
  336. }
  337. if(!cfGGetVar('updateApplication')) cfGSetVar('updateApplication','confirm');
  338.  
  339.  
  340.  
  341. /**
  342.  * Download confirmation page
  343.  */
  344.  
  345.  
  346. // If other file is passed
  347. $updateFile=false;
  348. if(isset($_GET['updateFile']) && eregi('^[0-9a-z/.]*$',$_GET['updateFile'])) $updateFile=$_GET['updateFile'];
  349.  
  350. cfInsertHEAD(false);
  351. echo '<meta width="440x155"></meta>';
  352. echo '<meta title="Weezo"></meta>';
  353. echo '</head>';
  354. echo '<body title="Weezo" oncontextmenu="return false;">';
  355. echo outDivFrame('frame2',false,'margin-top:10px');
  356.  
  357. echo outImage(outIcon('installIcon'),false,false,'float:left; margin-right:1em');
  358.  
  359. // If new version is beta version, display beta icon
  360. if(isset($_GET['beta'])) echo outImageIcon('beta');
  361.  
  362. // Update captions
  363. echo cfCaption('newVersionAvailable');
  364. echo '<br/><br/>'.cfCaption('updateVersionUpdate').'<br><br><br>';
  365. echo '<form name="dontAskForm" method="POST" action="">';
  366.  
  367. // Save / Cancel buttons
  368.     echo '<div style="position:relative; top:-4px; text-align:center; width:100%">'.outButton(cfCaption('genOK'),'javascript:dl();',outIcon('ok'));
  369.     echo '<span style="padding-left:2em;"></span>'.outButton(cfCaption('genCancel'),'javascript:doCancel();',outIcon('cancel'));
  370.     echo '</div></div>';
  371.  
  372.     echo '<input type="hidden" name="savePrefs" style="display:none;" />';
  373.     echo '<input type="hidden" name="doUpdate" style="display:none;" value=""/>';
  374.     if($updateFile) echo '<input type="hidden" name="updateFile" style="display:none;" value="'.$updateFile.'"/>';
  375.     // "Don't ask" Checkbox
  376.     echo '<input type="checkbox" name="dontAsk" style="margin-top:3px; margin-right:1em;" /><span>'.cfCaption('dontAskAnymore').'</span>';
  377. echo '<span style="position:absolute; right:1em"><a class="link" href="http://www.weezo.net/index.php?option=com_content&task=view&id=20&Itemid=48" target="_blank">'.cfCaption('updateNews').'</a></span>';
  378. echo '</form>';
  379.  
  380. ?>
  381. <script type="text/javascript" language="javascript">
  382. function dl(){
  383.     if(dgn("dontAsk").checked) dgn("savePrefs").value="auto";
  384.     dgn('doUpdate').value="download";
  385.     document.dontAskForm.submit();
  386. }
  387. function doCancel(){
  388.     if(!dgn("dontAsk").checked) wl.UICommand('close')
  389.     else {
  390.         dgn("savePrefs").value="disabled";
  391.         document.dontAskForm.submit();
  392.     }
  393. }
  394. </script>
  395. </body></html>